#!/usr/bin/perl -w
use strict;
use Getopt::Long;
my $exec_dir = "./";
my $exec_file = $exec_dir . "matrix_extract";
#88888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888
my $usage = <<"USAGE";

USAGE:

    test.pl
		
		This script tests the programme in the directory

USAGE

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# mandatory


# optional parameters
my $help = undef;
my $verbose = undef;
GetOptions('help' => \$help, 'verbose' => \$verbose);

die $usage if ($help);
my $verbose_str = $verbose ? "--verbose" : "";

sub prepare_cmd
{
	#remove beginning white space and new lines
	$_[0] =~tr/\t\n/ /d;
	$_[0] =~ s/\s+/ /gm;
	if ($_[1])
	{
		print STDERR $_[0], "\n";
	}
}

sub run_cmd
{
	prepare_cmd(@_);
	system $_[0] and die "Failed [ $_[0] ]\n$!\n";
}




sub test_loop
{
	my ($format_i, $format_o) = @_;
	print STDERR "\tTesting going from format $format_i to $format_o and back...\n";
	
	my $cmd = <<"EXEC_END";
		$exec_file
			 --header
			 --input $format_i
			 --output $format_o
			 $verbose_str
			 < test_data.$format_i |
		$exec_file
			 --header
			 --input $format_o
			 --output $format_i
			 $verbose_str
			 > test.results
EXEC_END
		run_cmd($cmd);
		my $diff_cmd = "diff -s test_data.$format_i test.results";
		my $diff_res = `$diff_cmd`;
		#print STDERR $diff_res, "\n";
		unless ($diff_res =~ /are identical/)
		{
			print  STDERR $cmd, "\n";
			return 0;
		}
		return 1;
}

sub test_subset
{
	my ($format) = @_;
	print STDERR "\tTesting filtering by names for format $format...\n";
	my $cmd = <<"EXEC_END";
		$exec_file
			 --header
			 --input $format
			 --output p
			 --names_filter test_data.names.subset
			 --alpha
			 $verbose_str
			 < test_data.$format
			 > test.results
EXEC_END
	run_cmd($cmd);
	
	my $diff_cmd = "diff -s test_data.subset.p test.results";
	my $diff_res = `$diff_cmd`;
	#print STDERR $diff_res, "\n";
	unless ($diff_res =~ /are identical/)
	{
		print  STDERR $cmd, "\n";
		return 0;
	}
	return 1;
}


sub test_regex_split
{
	my ($format) = @_;
	print STDERR "\tTesting format $format ";
	print STDERR "regex filtering by [A-H]...\n";
	my $cmd = <<"EXEC_END";
		$exec_file
			 --header
			 --input $format
			 --output p
			 --regex 'gene_[A-H]'
			 --alpha
			 $verbose_str
			 < test_data.$format
			 > test.results
EXEC_END
	run_cmd($cmd);
	
	$cmd = "diff -s test_data.AH.p test.results";
	my $diff_res = `$cmd`;
	#print STDERR $diff_res, "\n";
	unless ($diff_res =~ /are identical/)
	{
		return 0;
	}
		
	print STDERR "\t                 ";
	print STDERR "regex filtering by [I-Z]...\n";
	$cmd = <<"EXEC_END";
		$exec_file
			 --header
			 --input $format
			 --output p
			 --regex 'gene_[I-Z]'
			 --alpha
			 $verbose_str
			 < test_data.$format
			 > test.results
EXEC_END
	run_cmd($cmd);
	
	$cmd = "diff -s test_data.IZ.p test.results";
	$diff_res = `$cmd`;
	#print STDERR $diff_res, "\n";
	unless ($diff_res =~ /are identical/)
	{
		return 0;
	}
	
	return 1
}



for my $format1('p', 'l', 's', 'b')
{
	for my $format2('p', 'l', 's', 'b')
	{
		die "Error\n\t:Testing failed going from format $format1 to $format2 and back\n"
			unless test_loop($format1, $format2);
	}
}
for my $format1('p', 'l', 's', 'b')
{
	die "Error\n\t:Filtering by name testing failed going from format $format1 and back\n"
		unless test_subset($format1);

}
for my $format1('p', 'l', 's', 'b')
{
	die "Error\n\t:Regex testing failed going from format $format1 and back\n"
		unless test_regex_split($format1);
}


print STDERR "\n\tAll succeeded!!\n\n";
unlink ("test.results");